package ac.manchester.cs.afzal.autoservd.semantic_classes;


import ac.manchester.cs.afzal.autoservd.sql_handler.SQL_Handler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

public class SQL_Handler_SC extends SQL_Handler
{

    /**
     * Returns the SC instances from Occurrences table (BMC_2009), based on
     * the matching of terminological heads
     * @param value
     * @return
     */
    public Vector get_sc_instances_from_occurences(String value)
    {
        if (!(value.endsWith("s")) && !(value.equals("analysis")) 
                && !(value.equals("approaches")) && !(value.equals("repositories"))
                && !(value.equals("libraries"))&& !(value.equals("searches"))
                && !(value.equals("data")))
            value = value + "s";
        
        Vector instances = new Vector();
        String query;
        try
        {
            if (value.equals("libraries"))
                query = "SELECT distinct Value FROM occurrences where " +
                    "Value LIKE '%library' OR Value LIKE '%libraries'";
            else if (value.equals("repositories"))
                query = "SELECT distinct Value FROM occurrences where " +
                    "Value LIKE '%repository' OR Value LIKE '%repositories'";
            else if (value.equals("approaches"))
                query = "SELECT distinct Value FROM occurrences where " +
                    "Value LIKE '%approach' OR Value LIKE '%approaches'";
            else if (value.equals("searches"))
                query = "SELECT distinct Value FROM occurrences where " +
                    "Value LIKE '%search' OR Value LIKE '%searches'";
            else if (value.equals("data"))
                query = "SELECT distinct Value FROM occurrences where Value LIKE '%data'";
            else
                query = "SELECT distinct Value FROM occurrences where " +
                    "Value LIKE '%" + value + "' OR Value LIKE '%" + value + "s';";

            statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(query);
            while (rs.next())
            {
                instances.add(rs.getString("Value"));
            }
            statement.close();
        } catch (SQLException sqlex)
        {
            System.out.println("SQL Exception: Get Instances of Semantic Classes : " + sqlex.getMessage());
        }
        return instances;
    }

    /**
     * Creates the Semantic Class tables for each Semantic Class.
     * This method is obsolete
     * @param value
     */
    public void create_sc_tables(String value)
    {
        if (!(value.endsWith("s")) && !(value.equals("analysis"))
                && !(value.equals("approaches")) && !(value.equals("repositories"))
                && !(value.equals("libraries"))&& !(value.equals("searches"))
                && !(value.equals("data")))
            value = value + "s";

        try
        {
            statement = connection.createStatement();
            String query = "CREATE TABLE __sc_" + value + " (ID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,Value VARCHAR(100),PRIMARY KEY (ID))";
            statement.executeUpdate(query);
            statement.close();
        }
        catch (SQLException sqlex)
        {
            System.out.println("SQL Exception Creating Table : " + sqlex.getMessage());
        }
    }
 
}